package com.teach.controller.comments;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.R;
import com.teach.dto.CommentsSearch;
import com.teach.dto.LoginUser;
import com.teach.dto.NewsSearch;
import com.teach.entity.Comments;
import com.teach.entity.News;
import com.teach.service.CommentsService;
import com.teach.service.NewsService;
import com.teach.service.UserService;

/**
 * <pre>
 * 新闻评论的控制器
 * 创建时间 ：     2019年12月17日 下午10:09:13
 * 当前版本 ：     V1.0
 * 版权 ：            舒星所有
 * </pre>
 * @author 舒星
 *
 */
@Controller
@RequestMapping("/newsComments")
public class NewsCommentsController {
	@Autowired
	private CommentsService service;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private NewsService newsService;
	
	@RequestMapping("news-comments-json.do")
	@ResponseBody
	public  Object doList(Long commentCommentator,Integer current,Integer rowMax) {
		CommentsSearch search=new CommentsSearch();
		search.setCommentCommentator(commentCommentator);
		if(current!=null) {
			search.setCurrent(current);
		}
		if(rowMax!=null) {
			search.setRowMax(rowMax);
		}
		return service.commentsPage(search);
	}
	@RequestMapping("news-comments-add.do")
	@ResponseBody
	public Object doAdd(HttpServletRequest request,Long commentCommentator,String commentContent){
		try {
			Comments comments=new Comments();
			comments.setCommentCommentator(commentCommentator);
			LoginUser user=(LoginUser) request.getSession().getAttribute("user");
			comments.setCommentUserName(user.getUserName());
			comments.setCommentContent(commentContent);
			comments.setCommentType("新闻");
			String remoteAddr = request.getRemoteAddr();
		    String forwarded = request.getHeader("X-Forwarded-For");
		    String realIp = request.getHeader("X-Real-IP");
		    String ip = null;
		    if (realIp == null) {
		        if (forwarded == null) {
		            ip = remoteAddr;
		        } else {
		            ip = remoteAddr + "/" + forwarded.split(",")[0];
		        }
		    } else {
		        if (realIp.equals(forwarded)) {
		            ip = realIp;
		        } else {
		            if(forwarded != null){
		                forwarded = forwarded.split(",")[0];
		            }
		            ip = realIp + "/" + forwarded;
		        }
		    }
			comments.setCommentIp(ip);
			service.commentsInsert(comments);
			return R.ok();
		}catch(Exception e) {
			return R.error("发表评论失败");
		}	
	}
	@RequestMapping("news-comments-delete.do")
	@ResponseBody
	public Object doDelete(Long commentId) {
		try {
			service.commentsRemove(commentId);
			return R.ok();
		}catch(Exception e) {
			return R.error("删除评论失败");
		}
		
	}
	@RequestMapping("news-comments-delete2.do")
	@ResponseBody
	public Object doDelete(Long commentId,Boolean falg) {
		try {
			service.commentsRemove(commentId,falg);
			return R.ok();
		}catch(Exception e){
			return R.error("修改状态出错");
		}
		
	}
	@RequestMapping("my-comment.do")
	@ResponseBody
	public Object doListByUserName(Long commentCommentator,LoginUser user,Integer rowMax) {
		CommentsSearch search=new CommentsSearch();
		search.setCommentUserName(user.getUserName());
		if(rowMax!=null) {
			search.setRowMax(rowMax);
		}
		return service.commentsPage(search);
	}
	
	@RequestMapping("comment-realName.do")
	@ResponseBody
	public Object getCommentRealName(Long commentId) {
		return userService.get(service.get(commentId).getCommentUserName());
	}
	
	@RequestMapping("news-title.do")
	@ResponseBody
	public Object getNewsTitle(Long commentId,Boolean falg) {
		if(falg) {
			return newsService.get(service.get(commentId).getCommentCommentator());
		}else {
			return service.get(commentId);
		}
	}
	@RequestMapping("news-page-all.do")
	@ResponseBody
	public Object doPageAll(String userName,String title,Integer current) {
		CommentsSearch search =new CommentsSearch();
		if(current<=0&& current==null) {
			current=1;
		}
		search.setCurrent(current);
		if(userName!=null&&!"".equals(userName)) {
			search.setCommentUserName(userName);
		}
		if(title!=null&&!"".equals(title)) {
			search.setCommentCommentator(newsService.getByNewsTitle(title).getNewsId());
			
		}
		search.setCommentType("新闻");
		return service.commentsPageAll(search);
	}
}
